//
//  TableFootView.m
//  jingdong
//
//  Created by 陈明光 on 2021/7/25.
//  Copyright © 2021 陈明光. All rights reserved.
//

#import "TableFootView.h"
#import "YouLikeView.h"
#import "DBYScrollViewButton.h"
#import "CartScrollModel.h"
@interface TableFootView ()<DBYScrollViewDataSource,DBYScrollViewDelegate>
{
    NSMutableArray *_myArray;//模型
}
@property (nonatomic, strong) UILabel *money;
@property (nonatomic, strong) UIButton *commitBtn;

@property (nonatomic, strong) YouLikeView * youLikeView;
@end
@implementation TableFootView

- (instancetype)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
        self.backgroundColor = [UIColor whiteColor];
        //初始化数据
        [self initData];
        _money = [[UILabel alloc]init];
        _money.font = [UIFont systemFontOfSize:15];
        _money.textColor = [UIColor redColor];
        [self addSubview:_money];
        
        _commitBtn = [[UIButton alloc]init];
        [_commitBtn setTitle:@"选好了" forState:UIControlStateNormal];
        [_commitBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [_commitBtn addTarget:self action:@selector(commitBtnClick) forControlEvents:UIControlEventTouchUpInside];
        _commitBtn.backgroundColor = GeneralColor;
        [self addSubview:_commitBtn];
        
        [_money mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self).offset(20);
            make.width.equalTo(@100);
            make.height.equalTo(@15);
        }];
        [_commitBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self).offset(10);
            make.trailing.equalTo(self);
            make.height.equalTo(@50);
            make.width.equalTo(@100);
        }];
        
        //猜你喜欢View
        _youLikeView = [[YouLikeView alloc]init];
        _youLikeView.backgroundColor = [UIColor whiteColor];
        _youLikeView.dataSource = self;
        _youLikeView.delegate = self;
         //_youLikeView.cycleEnabled = NO;//如果设置为NO，则关闭循环滚动功能。
        [self addSubview:_youLikeView];
        [_youLikeView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.height.equalTo(@230);
            make.width.equalTo(self);
            make.top.equalTo(self.commitBtn.mas_top).offset(70);
            
        }];

    }
    return self;
}

#pragma mark 加载数据
-(void)initData{
    _myArray=[NSMutableArray arrayWithCapacity:0];
    for (int i=0; i<12; i++) {
        CartScrollModel * model = [[CartScrollModel alloc]init];
        switch (arc4random()%6) {
            case 0:
                model=[CartScrollModel initWithFirstTitle:@"联想（lenovo)" andPriceName:@"￥3899.00" andImageNumber:@"IMG_2206.PNG"];
                break;
            case 1:
                model=[CartScrollModel initWithFirstTitle:@"（printrite" andPriceName:@"￥39.00" andImageNumber:@"IMG_2203.PNG"];
                break;
            case 2:
                model=[CartScrollModel initWithFirstTitle:@"惠普（HP）M177fw彩" andPriceName:@"￥3330.00" andImageNumber:@"IMG_2205.PNG"];
                break;
            case 3:
                model=[CartScrollModel initWithFirstTitle:@"爱玛科MB-818智能无线蓝牙" andPriceName:@"￥199.00" andImageNumber:@"IMG_2207.PNG"];
                break;
            case 4:
                model=[CartScrollModel initWithFirstTitle:@"天色DR3150嗮鼓适应兄弟" andPriceName:@"￥439.00" andImageNumber:@"IMG_2211.PNG"];
                break;
            case 5:
                model=[CartScrollModel initWithFirstTitle:@"奥速V5剑客高清网络" andPriceName:@"￥99.00" andImageNumber:@"IMG_2223.PNG"];
                break;
                
                
            default:
                break;
        }
        
        
        [_myArray addObject:model];
    }
    
    
}

#pragma mark DBYScrollViewDataSource
-(UIView *)viewForDBYScrollView:(YouLikeView *)adScrollView atPage:(NSInteger)pageIndex{
    UIView *view = [adScrollView dequeueReusableView];//先获取重用池里面的
    
    if (!view) {//如果重用池里面没有就创建
        view = [[UIView alloc]init];
        NSMutableArray * btnArray = [[NSMutableArray alloc]init];
        for (int i=3*(int)pageIndex; i<3*(pageIndex+1); i++) {
            
            DBYScrollViewButton * button=[DBYScrollViewButton new];
            CartScrollModel * model=_myArray[i];
            [button scrollViewButtonWith:model.price Title:model.title Image:[UIImage imageNamed:model.image]];
            [view addSubview:button];
            [btnArray addObject:button];
        }
        [MasonyUtil equalSpacingView:btnArray
                           viewWidth:(view.size.width/3)
                          viewHeight:150
                      superViewWidth:view.size.width];
        
    }
    return view;
}

-(NSUInteger)numberOfViewsForDBYScrollView:(YouLikeView *)adScrollView{
    
    return _myArray.count/3;
}

#pragma mark DBYScrollViewDelegate
-(void)adScrollView:(YouLikeView *)adScrollView didClickedAtPage:(NSInteger)pageIndex{
    NSLog(@"-->>点击了:%@",[ _myArray objectAtIndex:pageIndex]);
}
-(void)adScrollView:(YouLikeView *)adScrollView didScrollToPage:(NSInteger)pageIndex{
    
    NSLog(@"--->>当前已展示:%@",[_myArray objectAtIndex:pageIndex]);
}


- (void)setSumMoney:(CGFloat)sumMoney{
    _sumMoney = sumMoney;
    self.money.text = [NSString stringWithFormat:@"共 ￥%.2lf",sumMoney];
}
- (void)commitBtnClick{
    if ([self.delegate respondsToSelector:@selector(didTableFootViewCommit)] ) {
        [self.delegate didTableFootViewCommit];
    }
}

@end
